﻿using TrueSync;

namespace HEFramework
{
    public static class PlayerSystem
    {
        [EntitySystem]
        public class PlayerAwakeSystem : AwakeSystem<Player>
        {
            protected override void Awake(Player _self)
            {
                var dc = _self.GetSceneChild<DataTableComponent>();
                if (dc == null)
                {
                    return;
                }

                _self.G = dc.LoadDataTable<DRGlobalSettings>().GetData((int)eGlobalSetting.Player_Start_G).Value1;
                _self.G_S = dc.LoadDataTable<DRGlobalSettings>().GetData((int)eGlobalSetting.Player_Start_G_S).Value1;
                _self.G_R = 100;
                _self.K = 0;
                _self.K_S = 0;
                _self.K_R = 100;
                _self.EXP_S = 0;
            }
        }


        #region 金币

        public static bool Change_G(this Player _self, FP _value)
        {
            FP value = _value;

            if (value > 0)
            {
                value = Formula.RA(value, _self.G_R);
                _self.G += value;
                int max = 9999999;
                if (_self.G > max)
                {
                    _self.G = max;
                }

                return true;
            }
            else if (value < 0)
            {
                //不满足
                if (_self.G + _value < 0)
                {
                    return false;
                }

                _self.G += _value;
                return true;
            }


            return true;
        }

        public static void Change_G_S(this Player _self, FP _value)
        {
            _self.G_S += _value;
        }

        public static void Change_G_R(this Player _self, FP _value)
        {
            _self.G_R += _value;
        }

        #endregion


        #region 杀敌数

        public static bool Change_K(this Player _self, FP _value)
        {
            FP value = _value;
            if (value > 0)
            {
                value = Formula.RA(value, _self.K_R);
                _self.K += value;
                int max = 9999999;
                if (_self.K > max)
                {
                    _self.K = max;
                }

                return true;
            }
            else if (value < 0)
            {
                //不满足
                if (_self.K + _value < 0)
                {
                    return false;
                }

                _self.K += _value;
                return true;
            }

            return true;
        }

        public static void Change_K_S(this Player _self, FP _value)
        {
            _self.K_S += _value;
        }

        public static void Change_K_R(this Player _self, FP _value)
        {
            _self.K_R += _value;
        }

        #endregion


        #region 经验

        public static void Change_EXP_S(this Player _self, FP _value)
        {
            _self.EXP_S += _value;
        }

        #endregion


        public static void PerSecond(this Player _self)
        {
            //金币每秒
            _self.Change_G(_self.G_S);
            //杀敌每秒
            _self.Change_K(_self.K_S);
            //经验每秒
            var temp = _self.GetSceneChild<Team>();
            var reward = _self.GetSceneChild<Reward>();
            if (temp == null || reward == null)
            {
                return;
            }

            for (int i = 0; i < temp.Heros.Count; i++)
            {
                var entity = temp.Heros[i].Entity;
                if (entity != null)
                {
                    reward.ChangeReward(entity, (int)eReward.EXP, _self.EXP_S);
                }
            }
        }
    }
}